function searchformsubmit()
{
    return false;
}
 function pageCreateEvent()
    {
        console.log('create FB event');
        var form = document.forms['postform'];
        var name = form.elements.name.value;
        var time = form.elements.time.value;
       // var picture = form.elements.fileField.value;
        var url = form.elements.url.value;
        var description = form.elements.description.value;
        var location = form.elements.location.value;
        var jsonObj=new Object();
        var tags = $('#tagresult').text();
        console.log(name);
        console.log(time);
        jsonObj.name = name;
        jsonObj.start_time = time+":00-0800";
        jsonObj.description =description;
        jsonObj.location  = location;
        jsonObj.url = url;
        
        
        FB.api(
            "/1409056866000177/events?fields=picture",
            "POST",
            {
                "name": name,
                //"start_time": "2014-03-01T13:00:00-0800",
                "start_time": time+":00-0800",
                "description" : description,
                "location":location,
                //"picture":"http://static.adzerk.net/Advertisers/63026cb250a04fa7affd046a12d19262.png"
            },
            function (response) {
              if (response && !response.error) {
                /* handle the result */
                  console.log(response);
               //send to database to save
               window.open("https://www.facebook.com/events/"+response.id+"/","_blank");
               
             createEventToServlet(response,jsonObj,tags);
              }
              else
               {
                   console.log(response);
                    console.log(response.id);
                    console.log(response.name);
                }
            }
        );
    }
    function postPhoto(photo){
        var img;
        var imgFile = document.getElementById('fileField');
        if (imgFile.files && imgFile.files[0]) {
            var width;
            var height;
            var fileSize;
            var reader = new FileReader();
            reader.onload = function(event) {
                var dataUri = event.target.result,
                img = document.createElement("img");
                img.src = dataUri;
                width = img.width;
                height = img.height;
                fileSize = imgFile.files[0].size;
                alert(img.src);
                alert(height);
                alert(fileSize);
                 FB.api(
                   "/612141232200555/photos",
                   "POST",
                   {
                       "source":"img.src"
                   }
                   ,function(response)
                   {
                       console.log(response);
                       if (response && !response.error) {
                           console.log('upload success');
                       }
                       else
                       {
                           console.log('upload error');
                       }
                   }
                   );
           };
           reader.onerror = function(event) {
               console.error("File could not be read! Code " + event.target.error.code);
           };
           reader.readAsDataURL(imgFile.files[0]);
        }
       
       
           
    }
    function send()
    { var form = document.forms['postform'];
    var picture = form.elements.fileField.value;
        postPhoto(picture);
    }
function createEventToServlet(response,jsonObj,tag)//after create FB event
{
    jsonObj.id = response.id;
    jsonObj.tag= tag;
    httpPost("createevent",jsonObj,getCreateEventResponse);
     //jqueryPost("createevent",{Action:'post','id':response.id,'url':form.url});
}
var getCreateEventResponse = function (data)
{
    console.log('get servlet response');
    alert("成功!!");
}
var getTag = function (data)
{
    console.log('get Tag servlet response');
    var obj = JSON.parse(data);
    rawTags =obj.tags;
    _tags = new Array(rawTags.length);
    
    tagResponseCount = 0;
    tagNum = _tags.length;
    console.log(tagNum);
    if(tagNum>0)
    {	
    	var s="<option value=\"\">Select one...</option>";
        for(var i=0;i<tagNum;i++)
        {
            s=s+"<option value=\""+rawTags[i].tag+"\">"+rawTags[i].tag+"</option>";
            console.log(s);
        }
        var combobox=document.getElementById('combobox');
        combobox.innerHTML=s;
    }
    
}
$(window).load(function(){
    $('#addtag').click(function()
    {
        var tag = $('#combobox').val();
       console.log(tag);
       if(!$('#tagresult').text())
       {
           $('#tagresult').text(tag);
       }
       else
        $('#tagresult').text($('#tagresult').text()+","+tag);
    });
    
    httpPost("gettag","123",getTag);
    
    
});
    
